package com.woniu.bh.common.sentinel.exception;

import com.woniu.bh.common.core.exception.ErrorResult;
import com.woniu.bh.common.core.exception.ProjectException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.time.LocalDateTime;

/**
 * @version: V1.0
 * @author: Xbao
 * @className: GlobalExceptionHandler
 * @packageName: com.woniu.project
 * @description: 全局异常处理
 * @data: 2020-09-27 21:36
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
    @ExceptionHandler(ProjectException.class)
    public ErrorResult errorHandler(ProjectException e){
        System.out.println("++++++++++++++++++++++++++++++++++++");
        e.printStackTrace();
        log.error("出现异常：{}，时间：{}，说明：{}",e.getClass().getName(),LocalDateTime.now().toString(),e.getMessage());
        return new ErrorResult(e.getCode(),e.getMessage());
    }

    @ExceptionHandler(Exception.class)
    public ErrorResult errorHandler(Exception e){
        System.out.println("---------------------------------");
        e.printStackTrace();
        log.error("出现异常：{}，时间：{}，说明：{}",e.getClass().getName(),LocalDateTime.now().toString(),e.getMessage());
        return new ErrorResult(500,e.getMessage());
    }
}
